/**
 * @param {ListNode} head
 * @return {boolean}
 */
// 方法一：标记法
// var hasCycle = function (head) {
//   while (head) {
//     if (head.flag === true) {
//       return true
//     }
//     head.flag = true
//     head = head.next
//   }
//   return false
// };

//方法二：快慢指针法
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
  if (!head || !head.next) return false

  let slow = head
  let fast = head
  while (fast && fast.next) {
    //慢指针移动一步，快指针移动两步
    slow = slow.next
    fast = fast.next.next
    //相遇说明有环
    if (slow === fast) {
      return true
    }
  }
  return false
};